package com.linsir.auth.modules.oauth.controller;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.ShearCaptcha;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.linsir.auth.modules.oauth.vo.CaptchaVo;
import com.linsir.auth.support.RedisOperator;
import com.linsir.core.code.ICode;
import com.linsir.core.controller.BaseController;
import com.linsir.core.vo.R;

import com.linsir.core.vo.Result;
import com.linsir.core.vo.jsonResults.JsonResult;
import lombok.RequiredArgsConstructor;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import static com.linsir.auth.constant.RedisConstants.*;


@RestController
@RequiredArgsConstructor
@RequestMapping("/v1/")
public class LoginController extends BaseController {

    private final RedisOperator<String> redisOperator;


    @GetMapping("getSmsCaptcha")
    public R<ICode,String> getSmsCaptcha(String phone) throws Exception {
        R result = null;

        result = exec("获取短信",()->{
             String smsCaptcha = "1234";
            // 存入缓存中，5分钟后过期
            redisOperator.set((SMS_CAPTCHA_PREFIX_KEY + phone), smsCaptcha, DEFAULT_TIMEOUT_SECONDS);
            return Result.SUCCESS(smsCaptcha);
        });
        return result;
    }


    @GetMapping("getCaptcha")
    public R<ICode,CaptchaVo> getCaptcha() throws Exception {
        // 生成一个唯一id
        R<ICode,CaptchaVo> result = null;
        result = exec("获取验证码",()->{
            // 使用huTool-captcha生成图形验证码
            // 定义图形验证码的长、宽、验证码字符数、干扰线宽度
            ShearCaptcha captcha = CaptchaUtil.createShearCaptcha(130, 34, 4, 2);
             long id = IdWorker.getId();
             redisOperator.set((IMAGE_CAPTCHA_PREFIX_KEY + id), captcha.getCode(), DEFAULT_TIMEOUT_SECONDS);
             CaptchaVo captchaVo =  new CaptchaVo(String.valueOf(id),captcha.getCode(),captcha.getImageBase64());
            return Result.SUCCESS(captchaVo);
        });
             return result;
    }




}
